<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Tree {

    static function build(array $elements, $parentId = NULL) {
        $branch = array();

        foreach ($elements as $element) {
            if ($element->parent == $parentId) {
                $children = self::build($elements, $element->id);

                $branch[$element->id]['text'] = CHtml::link((!empty($element->title) ? $element->title : $element->id0->title), array('update', 'id' => $element->id));
                if (isset($children)) {

                    $branch[$element->id]['children'] = $children;
                }
                // $branch[] = $element;
            }
        }

        return $branch;
    }

    static function sublink() {
        
    }
    public static $path = array();
    // $node is the ID of the node/page we want the path of 
           static function getPath($node) {

        // look up the parent of this node 

//        $result = mysql_query('SELECT parent FROM tree ' .
//                'WHERE title="' . $node . '";');
         
        $row = Sitemap::model()->find("id=$node");

        $pth=array();
       // var_dump($row);
       // exit();
        
        // only continue if this $node isn't the root node 
        // (that's the node with no parent) 

        if ($row->parent != '') {

            // the last part of the path to $node, is the name 
            // of the parent of $node 

            $pth[] = $row->parent;



            // we should add the path to the parent of this node 
            // to the path 

            self::$path = array_merge(self::getPath($row->parent), $pth);
        }


//        var_dump($path);
//                exit();
//        // return the path 

        return self::$path;
    }
    
    public static function getSibling($parentId){
        if($parentId!=Null)
        return Sitemap::model()->findAll("parent=".$parentId);
        else
         return Sitemap::model()->findAll("parent is null");   
    }

}

?>
